package com.vhuomai.wechat.service.impl;

import com.quanyue.auto.util.GlobalContext;
import com.vhuomai.wechat.entity.BaseWxAccount;
import com.vhuomai.wechat.mapper.WxAccountMapper;
import com.vhuomai.wechat.service.WxAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author harris
 */
@Service
public class WxAccountServiceImpl implements WxAccountService {
    
    @Autowired
    private WxAccountMapper accountMapper;
    
    @Override
    public BaseWxAccount findOne() {
        return accountMapper.findByMerchantNo(GlobalContext.merchantNo());
    }
    
    @Override
    public void update(BaseWxAccount account) {
        BaseWxAccount exist = this.findOne();
        if (exist == null) {
            account.setMerchantNo(GlobalContext.merchantNo());
            account.setCreateTime(LocalDateTime.now());
            account.setUpdateTime(account.getCreateTime());
            accountMapper.insert(account);
        } else {
            account.setId(exist.getId());
            account.setUpdateTime(LocalDateTime.now());
            accountMapper.updateSelective(account);
        }
    }
}
